﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Windsor;
using XWord.GuyWire.Configurators;
using Castle.MicroKernel.Registration;
using XWord.Common;

namespace XWord.GuyWire
{
    public interface IGuyWire
    {
        void Dewire();
        void Wire();
    }

    public class GeneralGuyWire : IGuyWire, IContainerAccessor
    {
        protected readonly List<IConfigurator> _configurators = new List<IConfigurator> 
                                                                        {
                                                                           new ComponentsConfigurator()                                                                          
                                                                        };

        private IWindsorContainer _container;

        public IWindsorContainer Container
        {
            get
            {
                if (_container == null)
                    _container = new WindsorContainer();
                return _container;
            }
        }

        public virtual void Wire()
        {
            Container.Register(Component.For<IEntityFactory>()
                             .ImplementedBy<EntityFactory>()
                             .LifeStyle.Singleton);
            _configurators.ForEach(c => c.Configure(Container));
        }

        public void Dewire()
        {
            if (_container != null)
                _container.Dispose();
        }

        protected void AddConfigurator(IConfigurator configurator)
        {
            _configurators.Add(configurator);
        }

        protected void RemoveConfigurator(IConfigurator configurator)
        {
            _configurators.Remove(configurator);
        }
    }
}
